package com.ruimei.controller;

import com.aliyuncs.exceptions.ClientException;
import com.ruimei.entitys.Parent;
import com.ruimei.entitys.Teacher;
import com.ruimei.service.PassWordService;
import com.ruimei.util.JSONUtils;
import com.ruimei.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Api(tags={"修改密码"})
public class PassWordController
{
  @Resource
  PassWordService passWordService;

  @Resource
  RedisTemplate<String,String> redisTemplate;


  @ApiOperation("修改教师密码")
  @PostMapping({"/teacher/password"})
  @ResponseBody
  public Map<String, Object> setPasswordByteacherId(String teacherCode, String newPwd, HttpServletRequest request)
  {
    Teacher teacher = (Teacher) JSONUtils.jsonStringToBean(redisTemplate.opsForValue().get(TokenUtils.getTeacherId(request)),Teacher.class);
    Map<String, Object> map = this.passWordService.setPasswordByteacherId(teacherCode, newPwd,teacher);
    
    return map;
  }
  
  @ApiOperation("教师获取验证码")
  @GetMapping({"/teacher/getcode"})
  @ResponseBody
  public Map<String, Object> getTeacherCode(HttpServletRequest request)
    throws ClientException
  {
    String teacherId = TokenUtils.getTeacherId(request);
    if(redisTemplate.hasKey(teacherId)){
      Teacher teacher = (Teacher) JSONUtils.jsonStringToBean(redisTemplate.opsForValue().get(teacherId),Teacher.class);
      Map<String, Object> map = this.passWordService.getTeacherCode(teacher);
      return map;
    }
            return null;


  }
  
  @ApiOperation("修改家长密码")
  @PostMapping({"/parent/password"})
  @ResponseBody
  public Map<String, Object> setPasswordByParentId(String parentCode, String newPwd,HttpServletRequest request)
  {
    String parentId = TokenUtils.getParentId(request);
    if(redisTemplate.hasKey(parentId)){
      Parent parent = (Parent) JSONUtils.jsonStringToBean(redisTemplate.opsForValue().get(parentId),Parent.class);
      Map<String, Object> map = this.passWordService.setPasswordByParentId(parent,parentCode, newPwd);
      return map;
    }

    return null;

  }
  
  @ApiOperation("家长端获取验证码")
  @GetMapping({"/parent/getcode"})
  @ResponseBody
  public Map<String, Object> getParentCode(HttpServletRequest request)
    throws ClientException
  {
    Map<String, Object> map = this.passWordService.getParentCode(TokenUtils.getParentId(request));
    return map;
  }

  @ApiOperation("获取验证码")
  @PostMapping({"/getCode"})
  @ResponseBody
  public Map<String, Object> getCode(String phoneNo,String scope) throws ClientException {
    Map<String, Object> map = new HashMap<>();
    if("parent".equals(scope)){
      Parent parent = this.passWordService.getParentByphoneNo(phoneNo);
      if(parent != null){
        map = this.passWordService.getCode(phoneNo);
      }
      else{
        map.put("status", false);
        map.put("message", "手机号不存在");
      }
    }else{
      Teacher teacher = this.passWordService.getTeacherByphoneNo(phoneNo);
      if(teacher != null){
        map = this.passWordService.getCode(phoneNo);
      }
      else{
        map.put("status", false);
        map.put("message", "手机号不存在");
      }
    }
    return map;
  }

  @ApiOperation("家长端忘记密码")
  @PostMapping({"/parent/getpassword"})
  @ResponseBody
  public Map<String, Object> p_getPassword(String phoneNo,String Code, String newPwd){
    Parent parent = this.passWordService.getParentByphoneNo(phoneNo);
    Map<String, Object> map = this.passWordService.setPasswordByParentId(parent,Code,newPwd);
    return map;
  }


  @ApiOperation("教师端忘记密码")
  @PostMapping({"/teacher/getpassword"})
  @ResponseBody
  public Map<String, Object> t_getPassword(String phoneNo,String Code, String newPwd){
    Teacher teacher = this.passWordService.getTeacherByphoneNo(phoneNo);
    Map<String, Object> map = this.passWordService.setPasswordByteacherId(Code, newPwd, teacher);
    return map;
  }

}

